package com.allwees.bs.c.module.order.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.allwees.bs.c.module.order.entity.LogisticsWaybillEntity;
import com.allwees.bs.c.module.order.entity.OrderItemEntity;
import com.allwees.bs.c.module.order.entity.OrderRefundEntity;
import com.allwees.bs.c.module.order.entity.OrderSkuSnapEntity;
import com.allwees.bs.core.model.order.wf.order.EOrderItemState;
import com.allwees.bs.core.model.order.wf.refund.ERefundState;
import com.allwees.core.common.constant.EPlatform;
import com.allwees.core.common.util.reflect.PojoUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@Data
@Accessors(chain = true)
//@ApiModel
public class OrderItemDTO implements Serializable {
    private String uuid;

    private String productUuid;

    private ProductSnapDTO product;

    private String skuUuid;

    private SkuSnapDTO sku;

    private Boolean reviewAble;

    private Boolean refundAble;

    // 可评价的最后时间
    private Date ratableAt;

    //是否可以收货
    private Boolean receiveAble;

    // 评价时间 默认为0
    private Date reviewAt;

    // 购买数量
    private Integer quantity;

    // 价格
    private BigDecimal price;

    //订单总金额
    private BigDecimal amt;

    //Sku总价
    private BigDecimal amtSku;

    //运费
    private BigDecimal amtShipping;

    // 退款状态
    private ERefundState refundState;

    // 退款金额
    private BigDecimal refundAmt;

    // 退款币种
    private String refundCurrency;

    // 退款时间
    private Date refundAt;

    // 退款类型
    private Integer refundType;

    // 退款原因
    private Integer refundReason;

    // 退款备注
    private String refundRemark;

    // 退款图
    private String[] refundPhotos;

    private OrderLogisticsDTO orderLogistic;

    //发货状态 true 已发货;false 未发货
    private boolean delivered;

    private BigDecimal  shippingAmtSkuAfterAdd;

    private BigDecimal productAmtSkuAfterAdd;

    private BigDecimal platformAmtSku;

    private BigDecimal amtCashSku;

    /**
     * 是否是新用户购买
     */
    @JsonIgnore
    @JSONField(serialize = false)
    protected Boolean newUser;

    @JsonIgnore
    @JSONField(serialize = false)
    private transient EPlatform platform;

    public OrderItemDTO() {
    }

    public OrderItemDTO(OrderItemEntity orderItem) {
        this();
        PojoUtil.copyWithOmit(this, orderItem, "order", "product", "sku");

        this.setReviewAble(orderItem.realReviewAble());
        this.setRefundAble(orderItem.realRefundAble());
    }

    public OrderItemDTO(OrderItemEntity orderItem, OrderSkuSnapEntity skuSnap, LogisticsWaybillEntity logistics) {
        this(orderItem);

        //FIXME - 此处应该使用o_order_snap_item构建ProductSnapDTO和SkuSnapDTO
//		long start = System.nanoTime();
//        this.setProduct(new ProductSnapDTO(BeanUtil.getBean(ProductServiceImpl.class).getProductByUuid(entity.getProductUuid()).filter(false)));
//		log.debug("entity.getProduct(): time: {}ms", (System.nanoTime() - start) / 1000000);
//		start = System.nanoTime();
//        this.setSku(new SkuSnapDTO(BeanUtil.getBean(ProductServiceImpl.class).getSkuByUuid(entity.getSkuUuid()).filter(false)));
//		log.debug("entity.getSku(): time: {}ms", (System.nanoTime() - start) / 1000000);

        this.setProduct(new ProductSnapDTO(skuSnap));
        this.setSku(new SkuSnapDTO(skuSnap));


        //  new OrderLogisticsDTO();

        this.delivered = logistics != null && !StringUtils.isBlank(logistics.getExpressCode());

        //this.orderLogistic = OrderLogisticsDTO.fromEntity(logistics);

        //是否可收货
        this.receiveAble =  EOrderItemState.DELIVERED.equals(orderItem.getState());
    }

    public OrderItemDTO setRefundInfo(OrderRefundEntity orderRefund) {
        if (orderRefund == null) {
            return this;
        }
        this.refundAmt = orderRefund.getRefundAmt();
        this.refundCurrency = orderRefund.getCurrency();
        this.refundType = orderRefund.getType();
        this.refundReason = orderRefund.getReason();
        this.refundRemark = orderRefund.getRemark();

        this.refundAt = orderRefund.getCreatedAt();
        this.refundState = orderRefund.getState();

        if (StringUtils.isBlank(orderRefund.getPhotos())) {
            return this;
        }

        this.refundPhotos = orderRefund.getPhotos().split(",");
        return this;
    }

    public String getRefundStateDesc() {
        if (refundState != null) {
            return refundState.getDesc();
        }
        return StringUtils.EMPTY;
    }
}
